package com.tuuzed.hydrogen.simple.quickstart;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tuuzed.hydrogen.Hydrogen;
import com.tuuzed.hydrogen.simple.R;
import com.tuuzed.hydrogen.simple.model.UserModel;

import java.util.Date;

public class UserActivity extends AppCompatActivity {
    public static final String EXTRA_USER = "USER";
    private EditText mEditId;
    private EditText mEditName;
    private EditText mEditSex;
    private EditText mEditAge;
    private EditText mEditPassword;
    private Button mBtnSave;
    private Button mBtnDelete;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        initView();
        final long userId = getIntent().getLongExtra(EXTRA_USER, -1);
        final UserModel user = UserModel.findById(UserModel.class, userId);
        if (user == null) {
            mBtnSave.setVisibility(View.VISIBLE);
            mBtnDelete.setVisibility(View.GONE);
        } else {
            mBtnDelete.setVisibility(View.VISIBLE);
            mEditId.setText(user.getId() + "");
            mEditName.setText(user.getName());
            mEditSex.setText(user.getSex());
            mEditAge.setText(user.getAge() + "");
            mEditPassword.setText(user.getPassword());
            mBtnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    user.delete();
                    finish();
                }
            });
        }
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel tmpUser = user;
                if (tmpUser == null) {
                    tmpUser = new UserModel();
                }
                String age = mEditAge.getText().toString();
                if (!TextUtils.isEmpty(age)) {
                    tmpUser.setAge(Integer.parseInt(age));
                }
                tmpUser.setName(mEditName.getText().toString());
                tmpUser.setPassword(mEditPassword.getText().toString());
                tmpUser.setSex(mEditSex.getText().toString());
                tmpUser.setCreateDate(new Date());
                tmpUser.save();
                finish();
            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mEditId = (EditText) findViewById(R.id.editId);
        mEditName = (EditText) findViewById(R.id.editName);
        mEditSex = (EditText) findViewById(R.id.editSex);
        mEditAge = (EditText) findViewById(R.id.editAge);
        mEditPassword = (EditText) findViewById(R.id.editPassword);
        mBtnSave = (Button) findViewById(R.id.btnSave);
        mBtnDelete = (Button) findViewById(R.id.btnDelete);
    }
}
